package com.hhss.qishi.controller.upload.ueditor;

import com.baidu.ueditor.ActionEnter;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 百度UEditor 文件上传控制器
 *
 * @Author :Administrator
 * @Description
 * @Date :2019-05-30 16:25.
 */
@RestController
@RequestMapping("ueditor")
public class UEditorController {


    @Value("${QSApplication.annex.uploadVirtualPath}")
    private String UPLOAD_VIRTUAL_PATH;

    @RequestMapping(value = "/config")
    public void config(HttpServletRequest request, HttpServletResponse response) {

        response.setContentType("application/json");

        /*String rootPath = request.getSession().getServletContext().getRealPath("/");*/
        // 系统路径
        /*String rootPath = this.getClass().getResource("/").getPath().replaceAll("^\\/", "");*/

        // 文件上传路径
        String rootPath = checkPath(UPLOAD_VIRTUAL_PATH);

        System.out.println("rootPath:" + rootPath);
        try {
            String exec = new ActionEnter(request, rootPath).exec();
            PrintWriter writer = response.getWriter();
            writer.write(exec);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String checkPath(String path){
        if(!StringUtils.isNullOrEmpty(path) && path.endsWith("\\")){
            path = path.substring(0, path.length()-1);
        }
        return path;
    }
}
